/* eslint-disable no-param-reassign */
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');

const app = express();

const PRODUCT_DATA_FILE = path.join(__dirname, 'server-product-data.json');
const CART_DATA_FILE = path.join(__dirname, 'server-cart-data.json');

app.set('port', (process.env.PORT || 3000));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use((req, res, next) => {
    res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
    res.setHeader('Pragma', 'no-cache');
    res.setHeader('Expires', '0');
    next();
});

// A fake API token our server validates
const API_TOKEN = 'D6W69PRgCoDKgHZGJmRUNA';

//从请求中获取token
const extractToken = (req) => (
    req.query.token
);


const authenticatedRoute = ((req, res, next) => {
    //从请求中获取token
    const token = extractToken(req);

    //判断token
    if (token) {
        if (token === API_TOKEN) {
            return next();
        } else {
            return res.status(403).json({
                success: false,
                error: 'Invalid token provided',
            });
        }
    } else {
        return res.status(403).json({
            success: false,
            error: 'No token provided. Supply token as query param `token`',
        });
    }
});

// 登录 500毫秒后 发送到客户端
// Make things more noticeable in the UI by introducing a fake delay
// to logins
const FAKE_DELAY = 500; // ms
app.post('/login', (req, res) => {
    setTimeout(() => (
        res.json({
            success: true,
            token: API_TOKEN,
        })
    ), FAKE_DELAY);
});

// get请求处理 返回json格式的产品数据
app.get('/products', authenticatedRoute, (req, res) => {
    fs.readFile(PRODUCT_DATA_FILE, (err, data) => {
        res.setHeader('Cache-Control', 'no-cache');
        res.json(JSON.parse(data));
    });
});


// get请求处理 返回json格式的购物车数据
app.get('/cart', authenticatedRoute, (req, res) => {
    fs.readFile(CART_DATA_FILE, (err, data) => {
        res.setHeader('Cache-Control', 'no-cache');
        res.json(JSON.parse(data));
    });
});

// post请求 加入购物车
app.post('/cart', (req, res) => {
    fs.readFile(CART_DATA_FILE, (err, data) => {
        const cartProducts = JSON.parse(data);
        const newCartProduct = {
            id: req.body.id,
            title: req.body.title,
            description: req.body.description,
            price: req.body.price,
            image_tag: req.body.image_tag,
            quantity: 1
        };
        let cartProductExists = false;
        cartProducts.map((cartProduct) => {
            if (cartProduct.id === newCartProduct.id) {
                cartProduct.quantity++;
                cartProductExists = true;
            }
        });
        if (!cartProductExists) cartProducts.push(newCartProduct);
        fs.writeFile(CART_DATA_FILE, JSON.stringify(cartProducts, null, 4), () => {
            res.setHeader('Cache-Control', 'no-cache');
            res.json(cartProducts);
        });
    });
});


// post请求 购物车物品-1
app.post('/cart/delete', (req, res) => {
    fs.readFile(CART_DATA_FILE, (err, data) => {
        let cartProducts = JSON.parse(data);
        cartProducts.map((cartProduct) => {
            if (cartProduct.id === req.body.id && cartProduct.quantity > 1) {
                cartProduct.quantity--;
            } else if (cartProduct.id === req.body.id && cartProduct.quantity === 1) {
                const cartIndexToRemove = cartProducts.findIndex(cartProduct => cartProduct.id === req.body.id);
                cartProducts.splice(cartIndexToRemove, 1);
            }
        });
        fs.writeFile(CART_DATA_FILE, JSON.stringify(cartProducts, null, 4), () => {
            res.setHeader('Cache-Control', 'no-cache');
            res.json(cartProducts);
        });
    });
});

// post请求 清空购物车
app.post('/cart/delete/all', (req, res) => {
    fs.readFile(CART_DATA_FILE, () => {
        let emptyCart = [];
        fs.writeFile(CART_DATA_FILE, JSON.stringify(emptyCart, null, 4), () => {
            res.json(emptyCart);
        });
    });
});


app.listen(app.get('port'), () => {
    console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});